<template>
  <div v-if="!isSearch" class=" .van-cell__value {
    display: flex;
  }">
    <!-- 头部 -->
    <div class="topempty">
      <TopSearch v-model="isSearch" class="top"></TopSearch>
    </div>
    
    <!-- tabs -->
    <div class="listenListNav">
      <!-- 控制显示隐藏按钮 -->
      <div class="button" @click="isShowHandler"><van-icon name="arrow-down" size="10px" v-if="!isShow" /> <van-icon
          name="arrow-up" color="orangered" size="10px" v-if="isShow" /></div>
      <!-- tag区头部 -->
      <div class="shadeTop" v-if="isShow">请选择分类</div>
      <!-- 弹出的tag区 -->
      <div class="shade" v-if="isShow">
        <span class="tag" v-for="(tagItem, index) in SubjectCategory" :key="tagItem.cid"
          @click="onClickTag(tagItem.name, index)">{{ tagItem.title }} </span>

      </div>
      <!-- 头部导航 -->
      <van-tabs v-model:active="active" color="#f06742" title-inactive-color="#a3a3ac" border @click-tab="onClickTab" class="tabs">
        <van-tab v-for="titleItem in SubjectCategory" :key="titleItem.cid" :title="titleItem.name">
          <template #title>
            <div class="title">{{ titleItem.title }}</div>
          </template>
          <!-- 内容区 -->

          <van-list v-model:loading="loading" :finished="finished" :immediate-check="false" finished-text="没有更多了"
            @load="onLoad(titleItem.name)" :offset="20">
         
            <van-cell class="bookList" v-for=" subjectItem in CategorySubjectList" :key="subjectItem.id">
               <a :href="`https://m.ximalaya.com/${subjectItem.url}`">
              <div class="bookItem">
               
                <img :src="subjectItem.coverPathSmall" :alt="subjectItem.title" class="messageImages">
                <div class="itemTitle">
                  <span class="itemTitleTop">{{ subjectItem.title }}</span>
                  <div class="itemTitleBottom">
                    <img src="./images/11.PNG">
                    <span class="itemTitleBottomText">{{ subjectItem.dataCount }}</span>
                    <span class="time">{{moment(subjectItem.releaseAt).format('YYYY-MM-DD')}}更新</span>
                  </div>
                </div>
               
              </div>
               </a>
            
            </van-cell>
          </van-list>
        </van-tab>
      </van-tabs>
    </div>
  </div>

  <div v-else class="search-page">
    <Search v-model="isSearch"></Search>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import moment from 'moment'
import listenListApi, { type SubjectCategoryModel, type QueryObject } from './api/listenList'
import TopSearch from '../../views/charts/components/TopSearch/TopSearch.vue'
import Search from '../../views/charts/components/Search/Search.vue'

let isSearch = ref(false)


let pageNum = 1
const pageSize = 10
let total = 0

//响应式数据
const isShow = ref(false)
const active = ref(0);
// const stop = ref(true)
const SubjectCategory = ref<SubjectCategoryModel[]>([])
const CategorySubjectList = ref<QueryObject[]>([])
const loading = ref(false);
const finished = ref(false);
//控制遮罩功能函数
function isShowHandler() {
  isShow.value = !isShow.value
}
//获取头部分类数据功能函数
async function getSubjectSategory() {
  try {
    let res = await listenListApi.findSubjectCategory()
    SubjectCategory.value = res.data.categories
  } catch (error) {
    console.log(error)

  }
}

// 获取内容列表数据
async function getCategorySubjectList(tittle: string) {
  try {
    let res = await listenListApi.findcategorySubjectList(tittle, pageNum, pageSize);
    let aaa = res.data.subjects
    total = res.data.totalCount
    if (aaa.length > 0) {
      //有数据
      finished.value = false
      if (aaa.length < pageNum) {
        // 数据小于一页数据
        finished.value = true
      }
      CategorySubjectList.value.push(...aaa)
      if (CategorySubjectList.value.length >= total) {
        finished.value = false
      }
    } else {
      finished.value = true
    }
  } catch (error) {
    console.log(error)
  }
}

//触底触发
const onLoad =  (name: string) => {
  setTimeout(()=>{
    pageNum = pageNum + 1;
   getCategorySubjectList(name);
  loading.value = false;
  },2000)
 
}

//tab点击
function onClickTab(itemList: any) {
  CategorySubjectList.value = []
  pageNum = 1
  finished.value = false
  getCategorySubjectList(itemList.title)
}

//点击tag
function onClickTag(name: string, index: number) {
  CategorySubjectList.value = []
  getCategorySubjectList(name)
  isShow.value = !isShow.value
  active.value = index
}



onMounted(() => {
  getSubjectSategory()
  getCategorySubjectList('youshengshu')

});
</script>

<style lang="less" scoped>

/deep/.van-tab--active {
    font-size: 18px;
    font-weight: 400;
  }

.main {
  .van-cell__value {
    display: flex;
    text-align: left
  }
}


.topempty{
    width: 375.33px;
  height: 52px;
  // background-color: orangered;
}
.top {
  background-color: white;
  position: fixed;
  z-index: 200;
}

.tab{
 

  .title {
  font-size: 15px;
  font-weight: 400;
}

}


.bookList {

  .bookItem {
    height: 100px;
    border-bottom: 1px solid #f3f4f5;
    display: flex;
    a{
      display: inline-block;
    }

    .messageImages {
      width: 70px;
      height: 70px;
      margin: 15px;

    }

  .itemTitle {
  text-align: left;
  height: 76px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  .itemTitleTop {
      width: 240px;
      display: inline-block;
      margin-top: 16px;
      font-size: 16px;
    }

    .itemTitleBottom {
      width: 240px;
      height: 16.64px;

      img {
        width: 12px;
        margin-right: 5px;
      }

      .itemTitleBottomText {
        font-size: 12px;
        color: #a4a4ad;
      }

      .time {
        display: inline-block;
        margin-left: 10px;
        font-size: 12px;
        color: #a4a4ad;
      }
    }
}
   

  }
}

.listenListNav {
  position: relative;

  .button {
    width: 40px;
    height: 43.99px;
    position: absolute;
    right: 0;
    top: 0;
    z-index: 100;
    text-align: center;
    line-height: 43.99px;
    color: #f3f4f5;
    background: white;
    border-left: 1px solid #f3f4f5;
    border-bottom: 1px solid #f3f4f5;



  }

  .shade {
    width: 360.33px;
    height: 345px;
    background: white;
    position: absolute;
    z-index: 101;
    padding: 7.5px;
    overflow: auto;


    .tag {
      width: 29.33%;
      height: 32px;
      margin: 7.5px 2%;
      text-align: center;
      line-height: 32px;
      background-color: #f3f4f5;
      border-radius: 4px;
      font-size: 16px;
      color: #000;
      display: inline-block;

    }

  }

  .shadeTop {
    widows: 100%;
    height: 44px;
    border-bottom: 1px solid #f3f4f5;
    line-height: 44px;
    padding-left: 10px;
    font-size: 14px;
    color: #a3a3ac;

  }
}



// ul{
//   height: 300px;
//   overflow-y:auto;
// }
</style>